package ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import model.TibNeeg;
import control.Constants;

/**
 * This class is a Panel and consist to display the TibNeeg, and take actions for modifications of this TibNeeg
 * on the panel parent.  
 * @author Anukun
 *
 */
public class UIDisplay_Tibneeg extends JPanel implements MouseListener{

	/**
	 * Default UID
	 */
	private static final long serialVersionUID = 1L;
	
	private TibNeeg kuv_tn;
	private UIFamilyTree family_tree_ui;
	private UIPupMenu kuvPopUp;
	
	private JPanel rootLabel;
	private BufferedImage logoTibNeeg; 
	

	
	public UIDisplay_Tibneeg(TibNeeg tn, UIFamilyTree family_tree_ui) {
		this.family_tree_ui = family_tree_ui;
		this.kuv_tn = tn;
		this.kuvPopUp = new UIPupMenu(this, kuv_tn, this.family_tree_ui.getYawmSaub());
		
		try {
			this.logoTibNeeg = ImageIO.read(new File("images/TibNeeg3.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		this.setPreferredSize(new Dimension(Constants.EACH_CHILD_WIDTH-10, Constants.EACH_CHILD_HEIGTH-30));
		//this.setPreferredSize(new Dimension(120, 170));
		//this.setBorder(BorderFactory.createEtchedBorder());
		
		//rootLabel = new JLabel(this.kuv_tn.getNpe(), SwingConstants.CENTER);
		
		
		rootLabel = new JPanel(){
			
			@Override
			public void paint(Graphics g) {
				//super.paint(g);
				
				Graphics2D g2d = (Graphics2D) g.create();
				Font font = new Font("Verdana", Font.BOLD, 12);
				g2d.setFont(font);
				
				FontRenderContext context = g2d.getFontRenderContext();
				
				TextLayout txt = new TextLayout(kuv_tn.getNpe(), font, context);
				Rectangle2D rect =  txt.getBounds();
				
				g2d.setColor(Constants.LABEL_DEFAULT_BACKGROUND);
				//g2d.fillRect(1, 1, getWidth()-3, getHeight()-3);
				
				g2d.setColor(Color.BLACK);
				int stringX = (int) ((getWidth() - rect.getWidth())/2.0);
				//int stringY = (int) ((getHeight()+rect.getHeight())/2.0);
				int stringY = (int) (6.0*(getHeight()/7.0));
				
				g2d.drawString(kuv_tn.getNpe(), stringX, stringY);
				
				//drawDisPlayRect(this, g2d, Constants.COLOR_RECT_DEFAULT);
				
				int ImageWidth = logoTibNeeg.getWidth();
				g2d.drawImage(logoTibNeeg, null, (int) ((getWidth()/2.0)-(ImageWidth/2.0)), 0);
				g2d.dispose();
				
			}
		};
		
		rootLabel.setPreferredSize(new Dimension(Constants.EACH_CHILD_WIDTH - 10, Constants.EACH_CHILD_HEIGTH - 40));
		//rootLabel.setPreferredSize(new Dimension(Constants.EACH_CHILD_WIDTH , Constants.EACH_CHILD_HEIGTH ));
		rootLabel.addMouseListener(this);
		
   		this.add(rootLabel);
   		
	}
	

	/**
	 * Draw the rectangle of npe label
	 * @param pan
	 * @param g2d
	 */
	private void drawDisPlayRect(JPanel pan, Graphics2D g2d, Color color) {
		g2d.setColor(color);
		g2d.drawLine(0, 0, 0, pan.getHeight()-2);
		g2d.drawLine(0, 0, pan.getWidth()-2, 0);
		g2d.drawLine(pan.getWidth()-2, 0, pan.getWidth()-2, pan.getHeight()-2);
		g2d.drawLine(0, pan.getHeight()-2, pan.getWidth()-2, pan.getHeight()-2);
	}
	
	
	@Override
	public void mouseClicked(MouseEvent e) {
		if( e.getButton() == MouseEvent.BUTTON3){
			
			Point point = rootLabel.getMousePosition();
			kuvPopUp.show(e.getComponent(), (int)point.getX(), (int)point.getY());
			
		}
	}


	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		//paint(Constants.LABEL_ENTER_BACKGROUND);	// Change background color when mouse entered
		Graphics2D g2d = (Graphics2D) rootLabel.getGraphics();
		//drawDisPlayRect(rootLabel, g2d, Constants.COLOR_RECT_ENTERED);
		//family_tree_ui.choseColorPan(rootLabel,kuv_tn, Constants.LABEL_ENTER_BACKGROUND);
	}

	@Override
	public void mouseExited(MouseEvent e) {	
		//paint(Constants.LABEL_DEFAULT_BACKGROUND); // Change background color when mouse exited
		Graphics2D g2d = (Graphics2D) rootLabel.getGraphics();
		//drawDisPlayRect(rootLabel, g2d, Constants.COLOR_RECT_DEFAULT);
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		//paint(Constants.LABEL_ENTER_BACKGROUND);	// Change background color when mouse entered
		
		//family_tree_ui.choseColorPan(rootLabel,kuv_tn, Constants.LABEL_SELECTED_BACKGROUND);
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	private void paint(Color color) {
		// TODO Auto-generated method stub
		Graphics2D g2d = (Graphics2D) rootLabel.getGraphics();
		
		Font font = new Font("Verdana", Font.BOLD, 12);
		g2d.setFont(font);
		
		FontRenderContext context = g2d.getFontRenderContext();
		TextLayout txt = new TextLayout(kuv_tn.getNpe(), font, context);
		Rectangle2D rect =  txt.getBounds();
		
		// Fill rectangle 
		g2d.setColor(color);
		g2d.fillRect(1, 1, rootLabel.getWidth()-3, rootLabel.getHeight()-3);
		
		// Draw string (Npe)
		g2d.setColor(Color.BLACK);
		int stringX = (int) ((rootLabel.getWidth() - rect.getWidth())/2.0);
		int stringY = (int) ((rootLabel.getHeight()+rect.getHeight())/2.0);
		g2d.drawString(kuv_tn.getNpe(), stringX, stringY);
		
	}
	
}
